package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserPageantData;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserPageantDaoImpl extends TempDataObjectDao<UserPageantData> {


    /**
     * 添加初始化数据
     */
    @Override
    public boolean addData(UserPageantData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_activity_pageant(" +
                "userId," +
                "beautyId," +
                "activityId," +
                "physicalStrength," +
                "score," +
                "singleHighScore," +
                "highScore," +
                "paint," +
                "strengthTime," +
                "lockBeauty," +
                "goodsTimes," +
                "lastUpdateTime," +
                "todayGetMoney," +
                "resetTime" +
                ") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getBeautyId());
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getPhysicalStrength());
            call.setInt(i++, data.getScore());
            call.setInt(i++, data.getSingleHighScore());
            call.setInt(i++, data.getHighScore());
            call.setString(i++, data.getPaint());
            call.setLong(i++, data.getStrengthTime());
            call.setString(i++, data.getLockBeauty());
            call.setString(i++, data.getGoodsTimes());
            call.setLong(i++, data.getLastUpdateTime());
            call.setInt(i++, data.getTodayGetMoney());
            call.setLong(i++, data.getResetTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }


    /**
     * 用户登陆加载数据
     */
    public Map<Integer,UserPageantData> getUserGameInfo(Long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,UserPageantData> userPageantDataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_pageant where userId =?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserPageantData result = parse(rs);
                userPageantDataMap.put(result.getActivityId(), result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userPageantDataMap;
    }


    @Override
    protected boolean updateData(UserPageantData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_activity_pageant SET beautyId=?,physicalStrength=?,score=?,singleHighScore=?,highScore=?,paint=?,strengthTime=?,lockBeauty=?,goodsTimes=?,lastUpdateTime=?,todayGetMoney=?,resetTime=? WHERE activityId=? and userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getBeautyId());
            call.setInt(i++, data.getPhysicalStrength());
            call.setInt(i++, data.getScore());
            call.setInt(i++, data.getSingleHighScore());
            call.setInt(i++, data.getHighScore());
            call.setString(i++, data.getPaint());
            call.setLong(i++, data.getStrengthTime());
            call.setString(i++, data.getLockBeauty());
            call.setString(i++, data.getGoodsTimes());
            call.setLong(i++, data.getLastUpdateTime());
            call.setInt(i++, data.getTodayGetMoney());
            call.setLong(i++, data.getResetTime());
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserPageantData parseData(ResultSet rs) throws SQLException {
        UserPageantData data = new UserPageantData();
        data.setUserId(rs.getLong("userId"));
        data.setBeautyId(rs.getInt("beautyId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setPhysicalStrength(rs.getInt("physicalStrength"));
        data.setScore(rs.getInt("score"));
        data.setSingleHighScore(rs.getInt("singleHighScore"));
        data.setHighScore(rs.getInt("highScore"));
        data.setPaint(rs.getString("paint"));
        data.setStrengthTime(rs.getLong("strengthTime"));
        data.setLockBeauty(rs.getString("lockBeauty"));
        data.setGoodsTimes(rs.getString("goodsTimes"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setTodayGetMoney(rs.getInt("todayGetMoney"));
        data.setResetTime(rs.getLong("resetTime"));
        return data;
    }

}
